package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDemo6 {
    public static void main(String[] args) throws SQLException {
        Connection connection = DBUtil.getConnection();
        Statement statement = connection.createStatement();
        String sql = "SELECT s.name, s.age, c.name, t.name " +
                     "FROM teacher t " +
                     "JOIN class c ON t.id = c.teacher_id " +
                     "JOIN student s ON c.id = s.class_id " +
                     "WHERE t.name = '刘苍松'";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            String stuName = resultSet.getString("s.name");
            int stuAge = resultSet.getInt("s.age");
            String clsName = resultSet.getString("c.name");
            String teaName = resultSet.getString("t.name");
            System.out.println(stuName + "," + stuAge + "," + clsName + "," + teaName);
        }
        connection.close();
    }
}
